package com.songcha.library_common.util

import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.songcha.library_common.LibraryCommon

import java.lang.reflect.Field

class StatusBarUtil {
    companion object{

        /***
         * navigationBar cannot hide,some phone only can set half translate
         */
        @JvmStatic
        fun fullScreenForever(context:Activity){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                val window: Window = context.window

                val attrs: WindowManager.LayoutParams = window.attributes
                attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                //window.attributes=attrs

                //fix some phone status bar become black

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    attrs.layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                }
                window.attributes=attrs

            }
        }


        @JvmStatic
        fun fullScreen(context:Activity){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                val window: Window = context.window
                val decorView = window.decorView

                val option = (
                        View.SYSTEM_UI_FLAG_FULLSCREEN
                                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        )
                decorView.systemUiVisibility=option

                //fix some phone status bar become black
                val lp: WindowManager.LayoutParams = window.attributes
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    lp.layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                    window.attributes=lp
                }

            }
        }

        @JvmStatic
        fun setTransparentStatusBar(context:Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.0
                val window: Window = context.window
                val decorView = window.decorView
                val option = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE

                        )
                decorView.systemUiVisibility =  decorView.systemUiVisibility or option
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window.statusBarColor = Color.TRANSPARENT
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //4.4
                val window: Window = context.window
                val attributes = window.attributes
                val flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                attributes.flags = attributes.flags or flagTranslucentStatus
                window.attributes = attributes
            }

        }

        @JvmStatic
        fun setStatusBarStyle(context:Activity,isDark:Boolean){
            //6.0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //val option =  context.window.decorView.systemUiVisibility
                val window: Window = context.window
                val decorView = window.decorView

                //SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

                if(isDark){
                    decorView.systemUiVisibility = decorView.systemUiVisibility or (View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

                } else{
                    decorView.systemUiVisibility = decorView.systemUiVisibility and (View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR).inv()

                }

                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window.statusBarColor = Color.TRANSPARENT

            }
        }

        /***
         * set all screen phone display mode
         */
        /*@JvmStatic
        fun setDisplayCutoutMode(activity: Activity){
            if(Build.VERSION.SDK_INT >= 28 && ScreenUtil.isAllScreenDevice(activity)){
                val lp=activity.window.attributes
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            }
        }*/

        @JvmStatic
        fun getStatusBarHeightByReflect():Int {
            var c: Class<*>? = null
            var obj: Any? = null
            var field: Field? = null
            var x = 0
            var top = 0
            try {
                c = Class.forName("com.android.internal.R${'$'}dimen")
                obj = c!!.newInstance()
                field = c.getField("status_bar_height")
                x = Integer.parseInt(field.get(obj).toString())
                top = LibraryCommon.getContext().resources.getDimensionPixelSize(x)
            } catch (e: Exception) {
                e.printStackTrace()
                top = 20.dp2px()
            }
            return top
        }

        @JvmStatic
        fun getStatusBarHeight(activity: Activity):Int {
            /*if (Build.VERSION.SDK_INT >= 28) {
                val windowInsetsCompat = ViewCompat.getRootWindowInsets(activity.window.decorView) //获取 WindowInsets
                val insets = windowInsetsCompat?.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.statusBars()) //获取 Insets
                if(insets==null) return getStatusBarHeightByReflect()
                return insets.top
            }else{
                return getStatusBarHeightByReflect()
            }*/

            val window = activity.window
            val context = activity

            val localRect = Rect()
            window.decorView.getWindowVisibleDisplayFrame(localRect)
            var statusBarHeight = localRect.top
            if (0 == statusBarHeight) {
                try {
                    val localClass = Class.forName("com.android.internal.R\$dimen")
                    val localObject = localClass.newInstance()
                    val i5 =
                        localClass.getField("status_bar_height")[localObject].toString().toInt()
                    statusBarHeight = context.resources.getDimensionPixelSize(i5)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
            if (0 == statusBarHeight) {
                try{
                    val resourceId: Int =
                        context.resources.getIdentifier("status_bar_height", "dimen", "android")
                    if (resourceId > 0) {
                        statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
                    }
                }catch (e:Exception){
                    e.printStackTrace()
                }
            }
            return statusBarHeight
        }


        /***
         * all screen phone is incorrect
         */
        @JvmStatic
        fun getBottomBarHeight():Int{
            var height = 0
            try{
                val resourceId: Int =
                    LibraryCommon.getContext().getResources().getIdentifier("navigation_bar_height", "dimen", "android")
                if (resourceId > 0) {
                    height =  LibraryCommon.getContext().getResources().getDimensionPixelSize(resourceId)
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
            return height
        }



    }
}